/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.task.setup;

import java.sql.SQLException;

import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.data.db.TaskDAO;
import edu.cmu.lti.sepia.data.db.UserDAO;
import edu.cmu.lti.sepia.infra.system.model.User;
import edu.cmu.lti.sepia.infra.topic.model.Task;
import edu.cmu.lti.sepia.util.ExceptionUtil;

public class SetUp {

	public static String setupProject( SQL sql, String projectName, String adminPassword ) {
		StringBuilder sb = new StringBuilder();
		sb.append( DBInitializer.createSharedDB( sql, projectName ) );
		sb.append( setupUser( sql, projectName, adminPassword ) );
		return sb.toString();
	}
	
	public static String setupTask( SQL sql, String projectName, Task task ) {
		StringBuilder sb = new StringBuilder();
		//Create tables
		sb.append( DBInitializer.createTaskDB( sql, projectName, task.getTaskNameShort() ) );
		
		//Add task to shared.task table
		try {
			sb.append( TaskDAO.addTask( sql, task.getTaskNameShort() ) );
		} catch ( SQLException e ) {
			sb.append( ExceptionUtil.exceptionToString(e) );
		}
		 
		return sb.toString();
	}
	
	private static String setupUser( SQL sql, String projectName, String adminPassword ) {
		StringBuilder sb = new StringBuilder();
		
		//Default settings
		User user = new User( -1, "admin", adminPassword, "admin", "EN", "Administrator" );
		
		//Add user to shared.user table
		try {
			sb.append( UserDAO.addUser( sql, user ) );
		} catch ( SQLException e ) {
			sb.append( ExceptionUtil.exceptionToString(e) );
		}
		 
		return sb.toString();		
	}
	
}
